package Interview.toutiao;

/**
 * 有序数组平方和的种数
 */
public class Test05 {
    public static void main(String[] args) {
        int[] nums = {0, 1, 2, 5};
        System.out.println(solution(nums));
    }

    public static int solution(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }

        int start = 0;
        int end = nums.length - 1;
        int result = 0;
        while (start <= end) {
            if (start > 0 && nums[start] == nums[start - 1]) {
                start++;
                continue;
            }

            if (end < nums.length - 1 && nums[end] == nums[end + 1]) {
                end--;
                continue;
            }

            int sum = nums[start] + nums[end];
            if (sum < 0) {
                start++;
            } else if (sum > 0) {
                end--;
            } else {
                start++;
                end--;
            }
            result++;
        }

        return result;
    }
}
